package com.ohuang.test_compose.composeView

import android.graphics.Point
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.Placeable

/**
 * 组件从下到上排列
 */
@Composable
fun ColumnForBottom2Top(
    modifier: Modifier = Modifier,
    verticalArrangement: ColumnForBottom2TopVerticalArrangement = ColumnForBottom2TopVerticalArrangement.Top,
    horizontalAlignment: Alignment.Horizontal = Alignment.Start,
    content: @Composable () -> Unit
){
    Layout(
        measurePolicy = ColumnForBottom2TopMeasurePolicy(verticalArrangement, horizontalAlignment),
        content = content,
        modifier = modifier
    )
}

enum class ColumnForBottom2TopVerticalArrangement{
     Top
    ,Bottom
    ,Center
    ,Top2 //会限制Column2高度
}

fun ColumnForBottom2TopMeasurePolicy(
    verticalArrangement: ColumnForBottom2TopVerticalArrangement = ColumnForBottom2TopVerticalArrangement.Top,
    horizontalAlignment: Alignment.Horizontal = Alignment.Start
) = MeasurePolicy { measurables, constraints ->
    val height = constraints.maxHeight
    val width = constraints.maxWidth
    val positions = arrayListOf<Point>()
    val placeables = arrayListOf<Placeable>()
    var xPosition = 0
    var yPosition = height
    val isInfiniteHeight = !constraints.hasBoundedHeight   //是否是无限高度  设置滚动的时候会为true
    for (placeable in measurables) {
        if (yPosition > 0) {
            var p: Placeable? = null
            p = if (isInfiniteHeight) {
                placeable.measure(constraints = constraints)
            } else {
                placeable.measure(constraints = constraints.copy(maxHeight = yPosition))
            }
            yPosition -= p.height
            xPosition = when (horizontalAlignment) {
                Alignment.Start -> 0
                Alignment.End -> width - p.width
                Alignment.CenterHorizontally -> (width - p.width) / 2
                else -> 0
            }
            positions.add(Point(xPosition, yPosition))
            placeables.add(p)
        } else {
            break
        }
    }


    return@MeasurePolicy if (isInfiniteHeight||(verticalArrangement==ColumnForBottom2TopVerticalArrangement.Top2)) {
        layout(constraints.maxWidth, height - yPosition) {
            positions.zip(placeables).forEach { (position, placeable) ->
                placeable.placeRelative(position.x, position.y - yPosition)
            }
        }
    } else {
        layout(constraints.maxWidth, constraints.maxHeight) {
            positions.zip(placeables).forEach { (position, placeable) ->
                when (verticalArrangement) {
                    ColumnForBottom2TopVerticalArrangement.Top -> placeable.placeRelative(position.x, position.y - yPosition)
                    ColumnForBottom2TopVerticalArrangement.Bottom -> placeable.placeRelative(position.x, position.y)
                    ColumnForBottom2TopVerticalArrangement.Center -> placeable.placeRelative(
                        position.x,
                        position.y - yPosition / 2
                    )
                    else -> placeable.placeRelative(position.x, position.y - yPosition)
                }

            }
        }
    }
}